/**
 * 应用树，主要用于显示树结构的应用、模块、功能数据，带右键菜单功能
 * json格式：[{"id":"AcApp#10","leaf":false,...},{...},...]
 * java类：com.augurit.common.system.common.MenuTreeNode 
 * @class Ext.ux.AppTree
 * @extends Ext.tree.TreePanel
 */
 Ext.namespace("Ext.ux");
 
 Ext.ux.AppTree = Ext.extend(Ext.tree.TreePanel,{
 
 	// 获取数据的URL
	url: '',
	
	// 是否使用箭头图标
	useArrows: true,
	
	// 是否显示动画
	animate: true,
	
	// 是否允许拖放
	enableDD: true,
	
	// 是否显示边框
	border: false,
	
	// 是否显示根节点
	rootVisible: true,
	
	// 是否自动显示滚动条
	autoScroll: true,
	containerScroll: true,
	
	// 设置间隔样式
	bodyStyle: 'padding:3px',
	
	// 组件初始化
	initComponent: function(){
	    
		// 根节点
    	this.root = new Ext.tree.AsyncTreeNode({
			id: "-1",	// 根节点的默认ID为“-1”
			text: '应用功能树',
			draggable: false,	// 不允许拖放
			leaf: false,	// 非叶子节点
			iconCls: 'icon-tree-root'
		});
		
		// 数据加载器
		this.loader = new Ext.tree.TreeLoader({
			dataUrl: this.url,
			listeners: {
				"beforeload": function(treeLoader, node){
					treeLoader.baseParams.nodeId = node.id;
				}
			}
		});
		
		// 调用父类初始化
		Ext.ux.AppTree.superclass.initComponent.call(this);
	    
		// 定义根节点的右键菜单
		var rootMenu = new Ext.menu.Menu({
			items: [{
				text: '新增应用',
				iconCls: 'add-child-menu',
				handler: this.addApp,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshApp,
				scope: this
			}]
		});
		
		//定义应用结点的右键菜单
		var appMenu = new Ext.menu.Menu({
			items:[{
				text: '新增顶级模块',
				iconCls:'icon-app-add',
				handler: this.addTopModule,
				scope: this
			},'-',{
				text: '删除应用',
				iconCls:'icon-app-delete',
				handler: this.deleteApp,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshApp,
				scope: this
			}]
		});
		
		//定义模块结点的右键菜单
		var moduleMenu = new Ext.menu.Menu({
			items:[{
				text: '新增子模块',
				iconCls: 'icon-app-add',
				handler: this.addChildModule,
				scope: this
			},'-',{
				text: '新增功能',
				iconCls:'icon-app-add',
				handler: this.addFunc,
				scope: this
			},'-',{
				text: '删除模块',
				iconCls: 'icon-app-delete',
				handler: this.deleteModule,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshApp,
				scope: this
			}]
		});
		
		//定义功能结点的右键菜单
		var funcMenu = new Ext.menu.Menu({
			items:[{
				text: '删除功能',
				iconCls: 'icon-app-delete',
				handler: this.deleteFunc,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshApp,
				scope: this
			}]
		});
		
		// 注册右键菜单及菜单实现
		this.on('contextmenu', contextMenu, this);
		function contextMenu(node, e) {
			e.preventDefault();
			node.select();
			if(node.id == '-1'){
				rootMenu.showAt(e.getXY());
			}else{
				if(node.id.indexOf('AcApplication#') == 0){
					appMenu.showAt(e.getXY());
				}else if(node.id.indexOf('AcModule#') == 0){
					moduleMenu.showAt(e.getXY());
				}
				else{
					funcMenu.showAt(e.getXY());
				}
			}
		}

		// 展开根节点
		this.getRootNode().expand();
 
	},
	
	//获取选中节点ID
	getSelectedNodeId: function(){
		var node = this.getSelectionModel().getSelectedNode();
		return node.id.split("#")[1];
	},
	
	//获取节点名称
	getSelectedNodeText: function(){
		var node = this.getSelectionModel().getSelectedNode();
		return node.text;
	},
	
	//节点刷新
	refreshApp: function(){
		var node = this.getSelectionModel().getSelectedNode();
		if(node == null || node.id == -1){
			this.root.reload();
		}else if(node.id.indexOf('AcFunction#') == 0){
			node.parentNode.reload();
		}else{
			node.reload();
		}
	},
	
	//刷新父节点
	refreshParentNode: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var parNode = node.parentNode;
		if(parNode != null){
			parNode.reload();
		}else{
			this.root.reload();
		}
	},
	
	//刷新父节点
	refreshRootNode: function(){
		this.root.reload();
	},
	
	//新建应用
	addApp: function(){
		var win = new Ext.ux.org.acapp.AppDetailWindow({id:'appDetailWindow', title:'新增应用'});
		win.show();
	},
	
	//删除应用
	deleteApp: function(){
	     Ext.MessageBox.show({
			title: '系统提示',
			msg: '确认要删除选择的应用吗？<br/><font color="red">注意</font>：删除应用将删除该应用下的所有模块和功能。',
			buttons: Ext.Msg.YESNO,
			icon: Ext.MessageBox.QUESTION,
			fn: this.confirmDeleteApp,
			width: 370,
			scope: this
		});
	},
	
	//执行删除应用操作
	confirmDeleteApp: function(btn){
		if(btn == 'yes'){
			var appId = this.getSelectedNodeId();
			
			var delMask = new Ext.LoadMask(Ext.getBody(), {msg: "正在执行删除操作，请稍候..."});    
		    delMask.show();
			        
			Ext.Ajax.request({    
				url: 'ac-application!delete.action',   
				success: function (result, request){    
					delMask.hide();
					delMask.destroy();
					
					this.refreshRootNode();
				},    
				failure: function (result, request){    
					delMask.hide();
					delMask.destroy();
					
					Ext.Msg.alert('系统提示', '删除操作失败！');
				},    
				method: 'POST',    
				params: {appIds:appId},
				scope: this   
			});
		}
	},
	
	//新增顶级模块
	addTopModule: function(){
		var appId = this.getSelectedNodeId();
		
		var win = new Ext.ux.org.acapp.ModuleDetailWindow({id:'moduleDetailWindow', title:'新增顶级模块', appId:appId});
		win.showAddTopModuleWin();
	},
	
	//新增子模块
	addChildModule: function(){
		var parentModuleId = this.getSelectedNodeId();
		var parentModuleName = this.getSelectedNodeText();
		
		var win = new Ext.ux.org.acapp.ModuleDetailWindow({id:'moduleDetailWindow', title:'新增子模块', parentModuleId:parentModuleId, parentModuleName:parentModuleName});
		win.showAddChildModuleWin();
	},
	
	//删除模块
	deleteModule: function(){
		Ext.MessageBox.show({
			title: '系统提示',
			msg: '确认要删除选中的模块吗？<br/><font color="red">注意</font>：删除模块将删除该模块下的所有子模块和功能。',
			buttons: Ext.Msg.YESNO,
			fn: this.confirmDeleteModule,
			width: 370,
			icon: Ext.MessageBox.QUESTION,
			scope: this
		});
	},
	
	//执行删除模块操作
	confirmDeleteModule: function(btn){
		if(btn == 'yes'){
			var moduleId = this.getSelectedNodeId();
			
			var delMask = new Ext.LoadMask(Ext.getBody(), {msg: "正在执行删除操作，请稍候..."});    
			delMask.show();
			        
			Ext.Ajax.request({
				url: 'ac-module!delete.action',
				success: function(result, request){
					delMask.hide();
					delMask.destroy();
					
					this.refreshParentNode();
				},
				failure:function(result, request){
					delMask.hide();
					delMask.destroy();
					
					Ext.Msg.alert('系统提示', '删除操作失败！');
				},
				method: 'post',
				params: {ids: moduleId},
				scope: this
			});
		}
	},
	
	//新增功能
	addFunc: function(){
		var moduleId = this.getSelectedNodeId();
	
		var win = new Ext.ux.org.acapp.FuncDetailWindow({id:'funcDetailWindow', title:'新增功能', moduleId:moduleId});
		win.showAddFuncWin();
	},
	
	//删除功能
	deleteFunc: function(){
		Ext.MessageBox.show({
			title: '系统提示',
			msg: '确认要删除选中的功能吗？<br/><font color="red">注意</font>：删除功能将同时删除与功能相关的菜单、角色关联信息。',
			buttons: Ext.Msg.YESNO,
			icon: Ext.MessageBox.QUESTION,
			fn: this.confirmDeleteFunc,
			width: 420,
			scope: this
		});
	},
	
	//执行删除功能操作
	confirmDeleteFunc: function(btn){
		if(btn == 'yes'){
			var funcId = this.getSelectedNodeId();
			
			var delMask = new Ext.LoadMask(Ext.getBody(), {msg: "正在执行删除操作，请稍候..."});    
			delMask.show();
			        
			Ext.Ajax.request({
				url: 'ac-function!delete.action',
				success: function(result, request){
					delMask.hide();
					delMask.destroy();
					
					this.refreshParentNode();
				},
				failure:function(result, request){
					delMask.hide();
					delMask.destroy();
					
					Ext.Msg.alert('系统提示', '删除操作失败！');
				},
				method: 'post',
				params: {ids: funcId},
				scope: this
			});
		}
	}
  
});